package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.ReportDrawingAuditPageQuery;
import com.eastfair.home.dto.ReportDrawingAuditSaveDTO;
import com.eastfair.home.dto.ReportDrawingAuditUpdateDTO;
import com.eastfair.home.entity.ReportDrawingAudit;
import com.eastfair.home.service.ReportDrawingAuditService;
import com.eastfair.home.vo.ReportDrawingAuditVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报馆图纸审核记录
 * </p>
 *
 * @author dq
 * @date 2022-08-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportDrawingAudit")
@Api(value = "ReportDrawingAudit", tags = "报馆图纸审核记录")
@PreAuth(replace = "home:reportDrawingAudit:")
public class ReportDrawingAuditController extends SuperController<ReportDrawingAuditService, Long, ReportDrawingAudit, ReportDrawingAuditPageQuery, ReportDrawingAuditSaveDTO, ReportDrawingAuditUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportDrawingAuditService reportDrawingAuditService;

    @Override
    public void handlerResult(IPage<ReportDrawingAudit> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportDrawingAudit> reportDrawingAuditList = list
                .stream()
                .map((map) -> ReportDrawingAudit.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportDrawingAuditList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportDrawingAuditPageQuery> params) {
        ReportDrawingAuditPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 报馆图纸分类审核ID查询报馆图纸审核VO列表.
     *
     * @param reportDrawingCategoryAuditId the report drawing category audit id
     * @return the r
     */
    @GetMapping("/reportDrawingCategoryAudit/{reportDrawingCategoryAuditId}")
    public R<List<ReportDrawingAuditVO>> queryByReportDrawingCategoryAuditId(@PathVariable("reportDrawingCategoryAuditId") Long reportDrawingCategoryAuditId) {
        log.info("queryByReportDrawingCategoryAuditId - 报馆图纸分类审核ID查询报馆图纸审核VO列表, reportDrawingCategoryAuditId={}", reportDrawingCategoryAuditId);
        return R.successDef(reportDrawingAuditService.listVOByReportDrawingCategoryAuditId(reportDrawingCategoryAuditId));
    }
}
